﻿String.prototype.trim=function()
{
    return this.replace(/(^\s*)|(\s*$)/g,'');
}
String.prototype.empty=function()
{
    return this.length == 0;
}
String.prototype.kick=function()
{
    return this.replace(/[\s~!@#$%^&*()_+|`\-=\\\{\}\[\]:";'<>?\,./]/g,'');
}

String.prototype.escape=function()
{
    var ret  = '';
    var cmap = {'~':'%7e','!':'%21','@':'%40','#':'%23','$':'%24','%':'%25','^':'%5e','&':'%26','*':'%2a','(':'%28',')':'%29','_':'%5f','+':'%2b','|':'%7c','`':'%60','-':'%2d','=':'%3d','\\':'%5c','{':'%7b','}':'%7d','[':'%5b',']':'%5d',':':'%3a','\"':'%22',';':'%3b','\'':'%27','<':'%3c','>':'%3e','?':'%3f',',':'%2c','.':'%2e','/':'%2f'};
    for(var i = 0; i < this.length; i++)
    {
        var c  = this.charAt(i);
        var mp = cmap[c];
        
        if ( mp == null )
        {
            ret += c;
        }
        else
        {
            ret += mp;
        }
    }
    
    return ret;
}

String.prototype.email=function()
{
    return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);
}

String.prototype.url=function()
{
    return /^(http|https):\/\//gi.test(this);
}

String.prototype.date=function()
{
    return Date.parse(this.replace(/-/g,'/'));
}

String.prototype.encodeHtml = function()
{ 
     return this.replace(/&/g,"&gt;")
        .replace(/</g,"&lt;")
        .replace(/>/g,"&gt;")
        .replace(/ /g,"&nbsp;")     
        .replace(/\'/g,"&#39;")  
        .replace(/\"/g,"&quot;")  
        .replace(/\n/g,"<br>");    
   
}

String.prototype.decodeHtml = function()
{ 
     return this.replace(/&gt;/g,"&")
        .replace(/&lt;/g,"<")
        .replace(/&gt;/g,">")
        .replace(/&nbsp;/g," ")     
        .replace(/&#39;/g,"\'")   
        .replace(/&quot;/g,"\"")  
        .replace(/<br>/g,"\n");    
   
}

String.prototype.domain = function()
{
    var pos = this.indexOf('//');
    if ( pos != -1 && (pos += '//'.length) < this.length)
    {
        pos = this.indexOf('/',pos);
        return this.substring(0,pos);
    }
    
    return this;
}

String.prototype.query = function()
{
    var query = {};
    
    var pos = this.indexOf('?');
	if (pos != -1 && ++pos < this.length) {
		var queryString = this.substring(pos);
		var items = queryString.split('&');
		for (var i = 0; i < items.length; i++) {
			var item = items[i];
			
			var name = item;
			var val  = '';
			pos = item.indexOf('=');
			if (pos != -1) {
				var name = item.substring(0, pos);
				if (++pos < item.length) {
					val = item.substring(pos);
				}
			}
			query[name] = val;
		}
	}
    
    return query;
}

function CallAjax(controller, action, rtype, rdata, rsuccess)
{
   var rurl = '/'+controller  + '/' + action;
   $.ajax
   (
        {
            type:rtype,
            url:rurl,
            dataType:'text/json',
            data:rdata,
            success:function(text)
            {
                var json = null;
                try
                {
                    json = eval('(' + text + ')');  
                    if ( json._redirect )
                    {
                        window.location.href=json._redirect;
                        return;
                    }   
                       
                    if ( json._error )
                    {
                        alert(json._error);
                        return;
                    }
                     
                    rsuccess(json);
                }
                catch(ex)
                {
                    alert(ex.description);
                }
            },
            error:function(httpReq,status,ex)
            {
                alert(ex.description);
            }
        }
   );  
}

var readyList = [];

function DocReady(fn)
{
    readyList.push(fn);
}

$(document).ready(function () {for(var i = 0; i < readyList.length; i++){readyList[i]();}});